<?php

declare(strict_types=1);

namespace App\Controller\Admin\V1\Admin;

use App\Controller\Admin\V1\Controllers;
use App\Request\Admin\Permission as PermissionRequest;
use App\Services\Admin\Menu as MenuServices;
use App\Services\Admin\Permission as PermissionServices;
use function Hyperf\Translation\__;

class Permission extends Controllers
{
    /**
     * 请求类
     * @var string
     */
    protected string $requestClass = PermissionRequest::class;

    public function __construct(
        PermissionServices $permissionServices,
        protected MenuServices $menuServices
    )
    {
        parent::__construct();
        $this->services = $permissionServices;
    }

    /**
     * 新增权限
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function save()
    {
        $input = $this->input(['name', 'menu_id', 'path', 'link', 'status', 'method', 'version']);
        $input['method'] = implode('|', $input['method']);
        $menuModel = $this->menuServices->find($input['menu_id']);
        if (!$menuModel) {
            return failed(__('message.not_found', ['name'=>__('model.table.menu')]));
        }
        if ($menuModel->level != 3) {
            return failed(__('message.level.parent', [
                'name' => __('model.table.menu'),
                'level' => 3,
                'contrast' => __('character.eq')
            ]));
        }
        $input['path'] = "/{$menuModel->app}/".strtolower($input['version'])."/".trim($input['path'], '/');
        $model = $this->services->create($input);
        if (!$model->exists) {
            return internalError('message.stored.failed');
        }

        return stored();
    }

    /**
     * 编辑
     * @param int $id
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function update(int $id)
    {
        $request = $this->container->get(PermissionRequest::class);
        $request->scene('update')->validateResolved();

        $input = $request->only(['name', 'menu_id', 'path', 'link', 'status', 'method', 'version']);
        $model = $this->services->findOrFail($id);
        $menuModel = $this->menuServices->find($input['menu_id']);
        if (!$menuModel) {
            return failed(__('message.not_found', ['name'=>__('model.table.menu')]));
        }
        if ($menuModel->level != 3) {
            return failed(__('message.level.parent', [
                'name' => __('model.table.menu'),
                'level' => 3,
                'contrast' => __('character.eq')
            ]));
        }
        $input['path'] = "/{$menuModel->app}/".strtolower($input['version'])."/".trim($input['path'], '/');
        $input['method'] = implode('|', $input['method']);
        if (!$model->fill($input)->save()) {
            return internalError('message.updated.failed');
        }

        return updated();
    }

    /**
     * 批量删除
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function destroy()
    {
        $request = $this->container->get(PermissionRequest::class);
        $request->scene('destroy')->validateResolved();

        $input = $request->only(['ids']);
        $this->services->delete($input['ids']);

        return deleted();
    }
}
